package com.dsl.dslsuzhou.controller;

import com.dsl.dslsuzhou.common.Result;
import com.dsl.dslsuzhou.model.entity.dsl.ChartDsl;
import com.dsl.dslsuzhou.service.ChartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 图表控制器
 */
@RestController
@RequestMapping("/api/${api-module}/charts")
public class ChartController {
    
    private static final Logger logger = LoggerFactory.getLogger(ChartController.class);
    
    @Autowired
    private ChartService chartService;
    
    /**
     * 执行图表查询
     */
    @PostMapping("/run")
    public Result<Object> run(@RequestBody ChartDsl dsl) {
        try {
            logger.info("Executing chart query with DSL: {}", dsl);

            Object result = chartService.executeChart(dsl);

            if (result instanceof List) {
                logger.info("Chart query completed, result size: {}", ((List<?>) result).size());
            } else {
                logger.info("Chart query completed, result type: {}", result.getClass().getSimpleName());
            }

            return Result.success(result);

        } catch (Exception e) {
            logger.error("Error executing chart query", e);
            return Result.error("Query execution failed: " + e.getMessage());
        }
    }
}
